package com.anji.util;

import java.io.File;

import com.anji.util.Properties;

public class RunSetup {
	
	public static final String runDir = "runs";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if ( args.length != 2 ) {
			System.exit( -1 );
		}
		
		String runName = args[0];
		int numRuns = Integer.parseInt(args[1]);
		
		setup(runName, numRuns);

		
	}

	public static void setup(Properties props) {
		setup( props.getProperty("run.name")
				, props.getIntProperty("num.runs"));
	}
	
	public static void setup(String runName, int numRuns) {
		String path = "./" + runDir + "/" + runName;
		File file = new File(path);
		if (file.exists()) {
			delete(file);
		}
		file.mkdir();
		
		File chrDir = new File(path+"/chromosome");
		chrDir.mkdir();
		
		File dbDir = new File(path+"/db");
		dbDir.mkdir();
		
		File pres = new File(path+"/pres");
		pres.mkdir();
		
			pres = new File(path+"/pres/complexity");
			pres.mkdir();
			pres = new File(path+"/pres/fitness");
			pres.mkdir();
			pres = new File(path+"/pres/species");
			pres.mkdir();
		
		File run = new File(path+"/run");
		run.mkdir();
		
		for (int i = 1; i <= numRuns; i++) {
			path = "./" + runDir + "/" + runName + "_" + i;
			file = new File(path);
			if (file.exists()) {
				delete(file);
			}
			file.mkdir();
			
			chrDir = new File(path+"/chromosome");
			chrDir.mkdir();
			
			dbDir = new File(path+"/db");
			dbDir.mkdir();
			
			pres = new File(path+"/pres");
			pres.mkdir();
			
				pres = new File(path+"/pres/complexity");
				pres.mkdir();
				pres = new File(path+"/pres/fitness");
				pres.mkdir();
				pres = new File(path+"/pres/species");
				pres.mkdir();
			
			run = new File(path+"/run");
			run.mkdir();
		}
		
	}
	
	public static void delete(File f)  {
		if (f.isDirectory()) {
			for (File c : f.listFiles())
				delete(c);
		} 
		f.delete();
	}
}
